package com.ssy.lingxi.product.serviceImpl;

import com.ssy.lingxi.common.constant.product.PriceTypeEnum;
import com.ssy.lingxi.product.entity.CommodityPriceRecord;
import com.ssy.lingxi.product.entity.es.EsCommodity;
import com.ssy.lingxi.product.entity.es.EsCommodityUnitPricePic;
import com.ssy.lingxi.product.repository.CommodityPriceRecordRepository;
import com.ssy.lingxi.product.repository.PriceCurveSetRepository;
import com.ssy.lingxi.product.repository.es.EsCommodityRepository;
import com.ssy.lingxi.product.service.ITaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 品牌实现类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/22
 */
@Service
public class TaskServiceImpl implements ITaskService {

    @Resource
    private PriceCurveSetRepository priceCurveSetRepository;

    @Resource
    private EsCommodityRepository esCommodityRepository;

    @Resource
    private CommodityPriceRecordRepository commodityPriceRecordRepository;

    /**
     * 统计分析商品价格
     */
    @Override
    public void analyseCommodityPrice() {
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        //获取前一天的日期
        LocalDate date = LocalDate.now().plusDays(-1);
        //查询所有启用了价格统计设置的数据
        List<Object[]> priceCurveSetList = priceCurveSetRepository.findByStatusGroupByMemberIdAndMemberRoleId(true);
        if(priceCurveSetList != null && priceCurveSetList.size() > 0){
            priceCurveSetList.forEach(member -> {
                Long memberId = Long.valueOf(String.valueOf(member[0]));
                Long memberRoleId = Long.valueOf(String.valueOf(member[1]));
                //查询当前会员的所有商品信息
                List<EsCommodity> esCommodityList = esCommodityRepository.findByMemberIdAndMemberRoleIdAndPriceType(memberId, memberRoleId, PriceTypeEnum.Cash.getCode());
                if(esCommodityList != null && esCommodityList.size() > 0){
                    List<Long> commoditySkuIdList = new ArrayList<>();
                    esCommodityList.forEach(esCommodity -> {
                        List<EsCommodityUnitPricePic> unitPricePicList = esCommodity.getUnitPricePicList();
                        unitPricePicList.forEach(esUnitPricePic -> {
                            commoditySkuIdList.add(esUnitPricePic.getId());
                        });
                    });
                    //查询当前会员的所有商品是否存在统计过的价格
                    List<CommodityPriceRecord> existCommodityPriceRecordList = commodityPriceRecordRepository.findByCommoditySkuIdInAndDate(commoditySkuIdList, date);
                    Map<String, CommodityPriceRecord> commodityPriceRecordMap = existCommodityPriceRecordList.stream().collect(Collectors.toMap(commodityPriceRecord -> commodityPriceRecord.getCommoditySkuId() + "_" + commodityPriceRecord.getDate(), CommodityPriceRecord -> CommodityPriceRecord));

                    List<CommodityPriceRecord> commodityPriceRecordList = new ArrayList<>();
                    esCommodityList.forEach(esCommodity -> {
                        List<EsCommodityUnitPricePic> unitPricePicList = esCommodity.getUnitPricePicList();
                        Long commodityId = esCommodity.getId();
                        unitPricePicList.forEach(esUnitPricePic -> {
                            Long commoditySkuId = esUnitPricePic.getId();
                            double price = 0;
                            try{
                                Map<String, Double> unitPrice = esUnitPricePic.getUnitPrice();
                                Collection<Double> c = unitPrice.values();
                                Object[] obj= c.toArray();
                                Arrays.sort(obj);
                                price = Double.parseDouble(decimalFormat.format(obj[0]));
                            }catch (Exception e){
                                e.printStackTrace();
                            }

                            //数据库存在该商品当天的数据则更新，不存在则新增
                            CommodityPriceRecord commodityPriceRecord = commodityPriceRecordMap.get(commoditySkuId + "_" + date);
                            if(commodityPriceRecord != null){
                                commodityPriceRecord.setPrice(price);
                            }else{
                                commodityPriceRecord = new CommodityPriceRecord();
                                commodityPriceRecord.setCommodityId(commodityId);
                                commodityPriceRecord.setCommoditySkuId(commoditySkuId);
                                commodityPriceRecord.setPrice(price);
                                commodityPriceRecord.setDate(date);
                            }
                            commodityPriceRecordList.add(commodityPriceRecord);
                        });
                    });

                    if(commodityPriceRecordList.size() > 0){
                        commodityPriceRecordRepository.saveAll(commodityPriceRecordList);
                    }
                }
            });
        }
    }
}

